package com.example.mybatisplus.controller;

import com.example.mybatisplus.common.JsonResponse;
import com.example.mybatisplus.service.QuestionnaireService;
import com.example.mybatisplus.model.domain.Questionnaire;
import com.example.mybatisplus.service.UserService;
import org.mybatis.logging.Logger;
import org.mybatis.logging.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 *
 *  前端控制器
 *
 *
 * @author lxp
 * @since 2022-06-01
 * @version v1.0
 */
@RestController
@RequestMapping("/api/questionnaire")
public class QuestionnaireController {

    private final Logger logger = LoggerFactory.getLogger( QuestionnaireController.class );

    @Autowired
    private QuestionnaireService questionnaireService;

    @Autowired
    private UserService userService;


    @GetMapping("")
    public JsonResponse show(){
        List<Questionnaire> questionnaires = questionnaireService.list();
        List<Map<String,String>> result = new ArrayList<>();
        for(Questionnaire q : questionnaires){
            Map<String,String> temp = new HashMap<>();
            temp.put("id",q.getId().toString());
            temp.put("time",new SimpleDateFormat("yyyy-MM-dd").format(q.getTestTime()));
            temp.put("stu_name",userService.getById(q.getStuId()).getStuName());
            temp.put("status",q.getHeartStatus());
            temp.put("score",q.getScore().toString());
            result.add(temp);
        }

        return JsonResponse.success(result).setMessage("查询问卷信息成功");
    }

    /**
    * 描述：根据Id 查询
    *
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse getById(@PathVariable("id") Long id)throws Exception {
        Questionnaire  questionnaire =  questionnaireService.getById(id);
        return JsonResponse.success(questionnaire);
    }

    /**
    * 描述：根据Id删除
    *
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public JsonResponse deleteById(@PathVariable("id") Long id) throws Exception {
        questionnaireService.removeById(id);
        return JsonResponse.success(null);
    }


    /**
    * 描述：根据Id 更新
    *
    */
    @RequestMapping(value = "", method = RequestMethod.PUT)
    @ResponseBody
    public JsonResponse updateQuestionnaire(Questionnaire  questionnaire) throws Exception {
        questionnaireService.updateById(questionnaire);
        return JsonResponse.success(null);
    }


    /**
    * 描述:创建Questionnaire
    *
    */
    @RequestMapping(value = "", method = RequestMethod.POST)
    @ResponseBody
    public JsonResponse create(Questionnaire  questionnaire) throws Exception {
        questionnaireService.save(questionnaire);
        return JsonResponse.success(null);
    }



}

